package com.morningstar.flow.flowable.task;

import cn.hutool.core.convert.Convert;
import cn.hutool.extra.spring.SpringUtil;
import com.morningstar.flow.antvX6.common.Assign;
import com.morningstar.flow.dao.entity.FlowNode;
import com.morningstar.flow.service.FlowInstanceTimeLineService;
import com.morningstar.flow.service.FlowNodeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;

import static com.morningstar.flow.flowable.assigne.AssigneeStrategy.ASSIGN_REFUSE_TO_END;
import static com.morningstar.flow.flowable.assigne.AssigneeStrategy.ASSIGN_REFUSE_TO_NODE;
import static com.morningstar.flow.flowable.constants.VariableName.NAME_AUDIT;

/**
 * 判断审批任务结果 用户审批服务任务
 *
 * @author Admin
 */
@Slf4j
public class AuditServiceTask implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        ExecutionEntityImpl entity = (ExecutionEntityImpl) execution;
        Object variable = execution.getVariable(NAME_AUDIT);
        Boolean approveResult = Convert.toBool(variable);
        log.info("AuditServiceTask {} {} {}", entity.getActivityName(), entity.getActivityId(), approveResult);
        if (approveResult) {
            return;
        }
        // 审批节点不通过逻辑
        String activityId = entity.getActivityId();
        String processInstanceId = entity.getProcessInstanceId();
        String processDefinitionKey = entity.getProcessDefinitionKey();
        String userTaskActivityId = StringUtils.replace(activityId, "approve_service_task_", "");
        FlowNodeService flowNodeService = SpringUtil.getBean(FlowNodeService.class);
        Assign assign = flowNodeService.getAssignByDefinitionKey(processDefinitionKey, userTaskActivityId);
        if (StringUtils.isBlank(assign.getAssignRefuse())) {
            throw new RuntimeException("审批拒绝策略未配置");
        }
        RuntimeService runtimeService = SpringUtil.getBean(RuntimeService.class);
        // 结束流程
        if (StringUtils.equalsIgnoreCase(assign.getAssignRefuse(), ASSIGN_REFUSE_TO_END)) {
            FlowNode flowNode = flowNodeService.getEndNodeByDefinitionKey(processDefinitionKey);
            if (flowNode == null) {
                throw new RuntimeException("流程结束节点未配置");
            }
            runtimeService.createChangeActivityStateBuilder()
                    .processInstanceId(processInstanceId)
                    .moveActivityIdTo(activityId, flowNode.getNodeActivityId())
                    .changeState();
            return;
        }
        // 驳回到指定节点
        if (StringUtils.equalsIgnoreCase(assign.getAssignRefuse(), ASSIGN_REFUSE_TO_NODE)) {
            if (StringUtils.isBlank(assign.getAssignRefuseNode())) {
                throw new RuntimeException("审批拒绝驳回节点未配置");
            }
            // todo 前端生成流程节点方式后修改
            String newActivityId = "n_" + StringUtils.replace(assign.getAssignRefuseNode(), "-", "");
            runtimeService.createChangeActivityStateBuilder()
                    .processInstanceId(processInstanceId)
                    .moveActivityIdTo(activityId, newActivityId)
                    .changeState();
            FlowInstanceTimeLineService flowInstanceTimeLineService = SpringUtil.getBean(FlowInstanceTimeLineService.class);
            flowInstanceTimeLineService.taskReject(entity, -9999L, "审批不通过系统自动驳回");
            return;
        }
        throw new RuntimeException("审批拒绝策略配置错误");
    }
}
